package com.tql.mall.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tql.mall.common.constant.PageConstant;
import com.tql.mall.common.result.R;
import com.tql.mall.common.result.RC;
import com.tql.mall.common.util.AuthContextHolder;
import com.tql.mall.model.order.OrderInfo;
import com.tql.mall.model.order.ReturnInfo;
import com.tql.mall.order.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: tyk
 * @Date: 2023/5/26 17:35
 * @Description:
 */
@RestController
@RequestMapping("api/order/auth")
@Api("订单管理")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 提交订单并返回订单ID
     */
    @PostMapping("submitOrder")
    @ApiOperation("提交订单并返回订单ID")
    public R<Long> submitOrder(@RequestBody OrderInfo orderInfo,
                         HttpServletRequest req) {
        // 用户ID
        String userId = AuthContextHolder.getUserId(req);

        // 订单号
        String tradeNo = req.getParameter(PageConstant.TRADE_NO);

        // 校验订单
        R r = orderService.checkOrder(userId, tradeNo, orderInfo);
        if (!RC.SUCCESS.getCode().equals(r.getCode())) return r;

        // 设置用户ID
        orderInfo.setUserId(Long.parseLong(userId));
        Long orderId = orderService.submitOrder(orderInfo);

        // 删除流水号
        orderService.deleteTradeCode(userId);

        // 返回流水号
        return R.ok(orderId);
    }

    /**
     * 根据orderId查询我的订单
     */
    @GetMapping("{page}/{limit}")
    @ApiOperation("根据orderId查询我的订单")
    public R<IPage<OrderInfo>> getOrderList(@PathVariable Long page,
                          @PathVariable Long limit,
                          HttpServletRequest req) {

        // 用户ID
        String userId = AuthContextHolder.getUserId(req);
        // 分页参数
        Page<OrderInfo> pageInfo = new Page<>(page, limit);
        // 查询
        IPage<OrderInfo> res = orderService.getOrderList(userId, pageInfo);
        return R.ok(res);
    }

    /**
     * 商家搜索订单
     */
    @GetMapping("admin/{page}/{limit}/{shopId}")
    @ApiOperation("商家搜索订单")
    public R<IPage<OrderInfo>> getOrderListByShop(@PathVariable Long page,
                                @PathVariable Long limit,
                                @PathVariable Long shopId) {
        // 分页参数
        Page<OrderInfo> pageInfo = new Page<>(page, limit);
        // 查询
        return R.ok(orderService.getOrderListForShop(shopId, pageInfo));
    }

    /**
     * 退货
     */
    @PostMapping("returnGood")
    @ApiOperation("退货")
    private R<Void> returnGood(@RequestBody ReturnInfo returnInfo, HttpServletRequest req) {
        // 设置用户ID
        String userId = AuthContextHolder.getUserId(req);
        returnInfo.setUserId(Long.parseLong(userId));
        // 退货
        orderService.returnGood(returnInfo);
        return R.ok();
    }


}
